﻿using System.Data;
using JBeamLibrary.Tools;
using JBeamLibrary.VehicleParts.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace JBeamLibrary.VehicleParts.Elements
{
    public class ElementProperty
    {
        #region Properties

        public ElementProperties Property { get; set; }

        public ElementPropertyUse Use { get; set; }

        public object Value { get; set; }

        public Type ValueType { get { return ElementPropertyTypes.TypeMap[Property]; } }

        public string Name{get { return Property.ToString(); }}

        #endregion

        #region CTORs

        public ElementProperty()
        {
            Property = ElementProperties.None;
            Value = null;
            Use = ElementPropertyUse.Single;
        }

        public ElementProperty(ElementProperties property, ElementPropertyUse use, object value)
        {
            Property = property;
            Use = use;
            Value = value;
        }

        #endregion

        #region Methods

        public override string ToString()
        {
            return string.Format("{0} property '{1}': '{2}' of type '{3}'", Use, Property, Value, ValueType);
        }

        public void FromJProperty(JProperty prop)
        {
            Property = GroupHelper.GetPropertyFromName(prop.Name);
            Value = prop.Value.FromJBeam(ElementPropertyTypes.TypeMap[Property]);
        }

        public JProperty ToJProperty()
        {
            var propName = GroupHelper.GetNameFromProperty(Property);
            var propVal = Value.ToJBeam();
            return new JProperty(propName, propVal);
        }

        public static ElementProperty FromJProperty(JProperty prop, ElementPropertyUse use)
        {
            var property = new ElementProperty { Use = use };
            property.FromJProperty(prop);
            return property;
        }

        public static JProperty ToJProperty(ElementProperty elProp)
        {
            return elProp.ToJProperty();
        }

        #endregion
    }
}
